home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / INTERNET / SITES / RAND / DVIEW000.LZH / VIEW_IO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-09  |  11.7 KB  |  485 lines

  1. /********************************************************************
  2.  FILENAME: VIEW_IO.CPP
  3.  AUTHOR  : JAKE HILL
  4.  DATE    : 12/1/94
  5.  
  6.  Copyright (c) 1994 by Jake Hill:
  7.  If you use any part of this code in your own project, please credit
  8.  me in your documentation and source code.  Thanks.
  9. ********************************************************************/
  10.  
  11. #define C
  12.  
  13. #include "view.h"
  14. #include "trig.h"
  15.  
  16. #ifdef __GNUC__
  17. #include <osbind.h>
  18. #include <unistd.h>
  19. #else
  20. #include <tos.h>
  21. #endif
  22.  
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <process.h>
  26. #include <stdlib.h>
  27.  
  28. /* Some more yucky global variables. */
  29. short NumThings;
  30. short NumSegs;
  31. short NumSides;
  32. short NumLines;
  33. short NumNodes;
  34. short NumSectors;
  35. short NumSSectors;
  36. short NumVertexes;
  37. short BlockmapSize;
  38.  
  39. void SetView(short *, short *, short *, unsigned short *);
  40. void LoadThings(Directory_Entry *Dir, FILE *handle);
  41. void LoadSegs(Directory_Entry *Dir, FILE *handle);
  42. void LoadSides(Directory_Entry *Dir, FILE *handle);
  43. void LoadLines(Directory_Entry *Dir, FILE *handle);
  44. void LoadNodes(Directory_Entry *Dir, FILE *handle);
  45. void LoadSectors(Directory_Entry *Dir, FILE *handle);
  46. void LoadVertexes(Directory_Entry *Dir, FILE *handle);
  47. void LoadSSectors(Directory_Entry *Dir, FILE *handle);
  48.  
  49. #define fix_w(X)   (X) = ((((X) & 0xff) << 8) + (((X) >> 8) & 0xff))
  50.  
  51. #if 0
  52. void fix_w(short &pcword)
  53. {
  54.    short tmp;
  55.  
  56.    tmp = pcword;
  57.    pcword = ((tmp & 0xff) << 8) + ((tmp >> 8) & 0xff);
  58. }
  59. #endif
  60.  
  61. void fix_l(long *pclong)
  62. {
  63.    long tmp;
  64.  
  65.    tmp = *pclong;
  66.    *pclong = ((tmp & 0xff) << 24) + (((tmp >> 8) & 0xff) << 16) +
  67.              (((tmp >> 16) & 0xff) << 8) + ((tmp >> 24) & 0xff);
  68. }
  69.  
  70. /* Sets the graphics mode. */
  71. void setmode(unsigned short n)
  72. {
  73. }
  74.  
  75. /* This function opens the wadfile, determines if it is a PWAD
  76.  * or an IWAD, then reads in the resources for THINGS, LINES, SIDES,
  77.  * VERTEXES, SEGS, SSECTORS, NODES, and SECTORS.
  78.  */
  79.  
  80. #ifndef C
  81. void View::OpenWad(char *WadName, long Level)
  82. #else
  83. void OpenWad(char *WadName, long Level)
  84. #endif
  85. {
  86.    FILE *file, *handle;
  87.    short EntriesRead = 0;
  88.    unsigned long EntryOffset = 0;
  89.  
  90.    WAD_Header HEADER;
  91.    Directory_Entry DIRECTORY;
  92.  
  93.    if ((file = fopen(WadName, "rb")) == NULL) {
  94.       printf("ERROR:Could not open file %s.\n",WadName);
  95.       exit(-1);
  96.    }
  97.  
  98. #ifdef __GNUC__
  99.    file->_flag |= _IOBIN;   /* The GNU libraries don't like "rb" */
  100. #endif
  101.  
  102.    fread(&HEADER, sizeof(WAD_Header), 1L, file);
  103.  
  104.    fix_l(&HEADER.num_entries);
  105.    fix_l(&HEADER.foffset);
  106.  
  107.    if (strnicmp(HEADER.signature, "IWAD", 4) == 0) {
  108.       printf("Reading IWAD file.\n");
  109. /* We need to skip past the PLAYPAL, and DEMO stuff if in an IWAD. */
  110.       HEADER.foffset += 6L * sizeof(Directory_Entry);
  111.    } else if (strnicmp(HEADER.signature, "PWAD", 4) == 0) {
  112.       printf("Reading PWAD file.\n");
  113.    } else {
  114.       printf("This is not a valid WAD file!\n");
  115.       exit(-1);
  116.    }
  117.  
  118.    printf("This file contains %d directory entries.\n\n", HEADER.num_entries);
  119.    EntryOffset += HEADER.foffset + (Level * 11L * sizeof(Directory_Entry));
  120.  
  121.    handle = file;
  122.    while (EntriesRead < 11) {
  123.       fseek(handle, EntryOffset, SEEK_SET);
  124.       fread(&DIRECTORY, sizeof(Directory_Entry), 1L, file);
  125.  
  126.       fix_l(&DIRECTORY.foffset);
  127.       fix_l(&DIRECTORY.size);
  128.  
  129.       if (strnicmp(DIRECTORY.name, "THINGS", 6) == 0)
  130.          LoadThings(&DIRECTORY, handle);
  131.       else if (strnicmp(DIRECTORY.name, "LINEDEFS", 8) == 0)
  132.          LoadLines(&DIRECTORY, handle);
  133.       else if (strnicmp(DIRECTORY.name, "SIDEDEFS", 8) == 0)
  134.          LoadSides(&DIRECTORY, handle);
  135.       else if (strnicmp(DIRECTORY.name, "VERTEXES", 8) == 0)
  136.          LoadVertexes(&DIRECTORY, handle);
  137.       else if (strnicmp(DIRECTORY.name, "SEGS", 4) == 0)
  138.          LoadSegs(&DIRECTORY, handle);
  139.       else if (strnicmp(DIRECTORY.name, "SSECTORS", 8) == 0)
  140.          LoadSSectors(&DIRECTORY, handle);
  141.       else if (strnicmp(DIRECTORY.name, "NODES", 5) == 0)
  142.          LoadNodes(&DIRECTORY, handle);
  143.       else if (strnicmp(DIRECTORY.name, "SECTORS", 7) == 0)
  144.          LoadSectors(&DIRECTORY, handle);
  145.       else
  146.          printf("Skipping %8s.\n",DIRECTORY.name);
  147.  
  148.       EntriesRead++;
  149.       EntryOffset += sizeof(Directory_Entry);
  150.    }
  151.    printf("Allocated memory for arrays...\n");
  152.  
  153.    fclose(file);
  154.    InitTrig();
  155. }
  156.  
  157. /* All of the following procedures read in the indicated
  158.  * resource from a WAD file pointed to by handle.
  159.  */
  160.  
  161. #ifndef C
  162. void View::LoadThings( Directory_Entry *Dir, FILE *handle )
  163. #else
  164. void LoadThings(Directory_Entry *Dir, FILE *handle)
  165. #endif
  166. {
  167.    float angle;
  168. #ifdef C
  169.    short i;
  170.    thing *Thing_Array;
  171. #endif
  172.    short x, y, h, a;
  173.  
  174.    NumThings = (short)(Dir->size / sizeof(thing));
  175.    printf("THINGS   : %d\n", NumThings);
  176.  
  177. #ifndef C
  178.    thing *Thing_Array = new thing[NumThings];
  179. #else
  180.    if ((Thing_Array = malloc(Dir->size)) == NULL)
  181.       exit(-1);
  182. #endif
  183.  
  184.    fseek(handle, Dir->foffset, SEEK_SET);
  185.    fread(Thing_Array, (unsigned int)Dir->size, 1L, handle);
  186.  
  187. #ifndef C
  188.    for(short i = 0;i < NumThings;i++) {
  189. #else
  190.    for(i = 0;i < NumThings;i++) {
  191. #endif
  192.       fix_w(Thing_Array[i].x);
  193.       fix_w(Thing_Array[i].y);
  194.       fix_w(Thing_Array[i].angle);
  195.       fix_w(Thing_Array[i].thing_type);
  196.       fix_w(Thing_Array[i].attributes);
  197.       if (Thing_Array[i].thing_type == 1) {
  198.          angle = (float) (182.0444444444444 * (float)Thing_Array[i].angle);
  199.          x = Thing_Array[i].x;   /* Indirection is needed in another place */
  200.          y = Thing_Array[i].y;
  201.          h = 40;
  202.          a = angle;
  203.          SetView(&x, &y, &h, &a);
  204.          break;
  205.       }
  206.    }
  207. #ifndef C
  208.    delete [] Thing_Array;
  209. #else
  210.    free(Thing_Array);
  211. #endif
  212. }
  213.  
  214. #ifndef C
  215. void View::LoadSegs(Directory_Entry *Dir, FILE *handle)
  216. #else
  217. void LoadSegs(Directory_Entry *Dir, FILE *handle)
  218. #endif
  219. {
  220. #ifdef C
  221.    int i;
  222. #endif
  223.  
  224.    NumSegs = (short)(Dir->size / sizeof(seg));
  225.    printf("SEGS     : %d\n", NumSegs);
  226.  
  227. #ifndef C
  228.    Seg_Array = new seg[NumSegs];
  229. #else
  230.    if ((Seg_Array = malloc(Dir->size)) == NULL)
  231.       exit(-1);
  232. #endif
  233.  
  234.    fseek(handle, Dir->foffset, SEEK_SET);
  235.    fread(Seg_Array, (unsigned int) Dir->size, 1L, handle);
  236.  
  237. #ifndef C
  238.    for(int i = 0;i < NumSegs;i++) {
  239. #else
  240.    for(i = 0;i < NumSegs;i++) {
  241. #endif
  242.       fix_w(Seg_Array[i].from);
  243.       fix_w(Seg_Array[i].to);
  244.       fix_w(Seg_Array[i].angle);
  245. #ifdef __GNUC__
  246.       fix_w(Seg_Array[i].line);
  247. #else
  248.       fix_w(Seg_Array[i].xline);
  249. #endif
  250.       fix_w(Seg_Array[i].line_side);
  251.       fix_w(Seg_Array[i].line_offset);
  252.    }
  253. }
  254.  
  255. #ifndef C
  256. void View::LoadSides( Directory_Entry *Dir, FILE *handle )
  257. #else
  258. void LoadSides(Directory_Entry *Dir, FILE *handle)
  259. #endif
  260. {
  261. #ifdef C
  262.    int i;
  263. #endif
  264.  
  265.    NumSides = (short)(Dir->size / sizeof(side));
  266.    printf("SIDEDEFS : %d\n", NumSides);
  267.  
  268. #ifndef C
  269.    Side_Array = new side[NumSides];
  270. #else
  271.    if ((Side_Array = malloc(Dir->size)) == NULL)
  272.       exit(-1);
  273. #endif
  274.  
  275.    fseek(handle, Dir->foffset, SEEK_SET);
  276.    fread(Side_Array, (unsigned int)Dir->size, 1L, handle);
  277.  
  278. #ifndef C
  279.    for(int i = 0;i < NumSides;i++) {
  280. #else
  281.    for(i = 0;i < NumSides;i++) {
  282. #endif
  283.       fix_w(Side_Array[i].tm_xoffset);
  284.       fix_w(Side_Array[i].tm_yoffset);
  285.       fix_w(Side_Array[i].sector);
  286.    }
  287. }
  288.  
  289. #ifndef C
  290. void View::LoadLines( Directory_Entry *Dir, FILE *handle )
  291. #else
  292. void LoadLines(Directory_Entry *Dir, FILE *handle)
  293. #endif
  294. {
  295. #ifdef C
  296.    int i;
  297. #endif
  298.  
  299.    NumLines   = (short)(Dir->size / sizeof(line));
  300.    printf("LINEDEFS : %d\n", NumLines);
  301.  
  302. #ifndef C
  303.    Line_Array = new line[NumLines];
  304. #else
  305.    if ((Line_Array = malloc(Dir->size)) == NULL)
  306.       exit(-1);
  307. #endif
  308.  
  309.    fseek(handle, Dir->foffset, SEEK_SET);
  310.    fread(Line_Array, (unsigned int)Dir->size, 1L, handle);
  311.  
  312. #ifndef C
  313.    for(int i = 0;i < NumLines;i++) {
  314. #else
  315.    for(i = 0;i < NumLines;i++) {
  316. #endif
  317.       fix_w(Line_Array[i].from);
  318.       fix_w(Line_Array[i].to);
  319.       fix_w(Line_Array[i].flags);
  320.       fix_w(Line_Array[i].special);
  321.       fix_w(Line_Array[i].tag);
  322.       fix_w(Line_Array[i].side[0]);
  323.       fix_w(Line_Array[i].side[1]);
  324.    }
  325. }
  326.  
  327. #ifndef C
  328. void View::LoadNodes( Directory_Entry *Dir, FILE *handle )
  329. #else
  330. void LoadNodes(Directory_Entry *Dir, FILE *handle)
  331. #endif
  332. {
  333. #ifdef C
  334.    int i;
  335. #endif
  336.    NumNodes = (short)(Dir->size / sizeof(node));
  337.    printf("NODES    : %d\n", NumNodes);
  338.  
  339. #ifndef C
  340.    Node_Array = new node[NumNodes];
  341.    PNode_Array = new node * [NumNodes];
  342. #else
  343.    if ((Node_Array = malloc(Dir->size)) == NULL)
  344.       exit(-1);
  345.    if ((PNode_Array = malloc(NumNodes * sizeof(node *))) == NULL)
  346.       exit(-1);
  347. #endif
  348.    MaxNode  = NumNodes - 1;
  349.  
  350.    fseek(handle, Dir->foffset, SEEK_SET);
  351.    fread(Node_Array, (unsigned int)Dir->size, 1L, handle);
  352.  
  353. #ifndef C
  354.    for(int i = 0;i < NumNodes;i++) {
  355. #else
  356.    for(i = 0;i < NumNodes;i++) {
  357. #endif
  358.       fix_w(Node_Array[i].x);
  359.       fix_w(Node_Array[i].y);
  360.       fix_w(Node_Array[i].dx);
  361.       fix_w(Node_Array[i].dy);
  362.       fix_w(Node_Array[i].ry2);
  363.       fix_w(Node_Array[i].ry1);
  364.       fix_w(Node_Array[i].rx1);
  365.       fix_w(Node_Array[i].rx2);
  366.       fix_w(Node_Array[i].ly2);
  367.       fix_w(Node_Array[i].ly1);
  368.       fix_w(Node_Array[i].lx1);
  369.       fix_w(Node_Array[i].lx2);
  370.       fix_w(Node_Array[i].right);
  371.       fix_w(Node_Array[i].left);
  372.       PNode_Array[i] = &Node_Array[i];
  373.    }
  374. }
  375.  
  376. #ifndef C
  377. void View::LoadSectors(Directory_Entry *Dir, FILE *handle)
  378. #else
  379. void LoadSectors(Directory_Entry *Dir, FILE *handle)
  380. #endif
  381. {
  382. #ifdef C
  383.    int i;
  384. #endif
  385.    NumSectors = (short)(Dir->size / sizeof(sector));
  386.    printf("SECTORS  : %d\n", NumSectors);
  387.  
  388. #ifndef C
  389.    Sector_Array = new sector[NumSectors];
  390. #else
  391.    if ((Sector_Array = malloc(Dir->size)) == NULL)
  392.       exit(-1);
  393. #endif
  394.  
  395.    fseek(handle, Dir->foffset, SEEK_SET);
  396.    fread(Sector_Array, (unsigned int)Dir->size, 1L, handle);
  397.  
  398. #ifndef C
  399.    for(int i = 0;i < NumSectors;i++) {
  400. #else
  401.    for(i = 0;i < NumSectors;i++) {
  402. #endif
  403.       fix_w(Sector_Array[i].floor_ht);
  404.       fix_w(Sector_Array[i].ceiling_ht);
  405.       fix_w(Sector_Array[i].light);
  406.       fix_w(Sector_Array[i].type);
  407.       fix_w(Sector_Array[i].trigger);
  408.    }
  409. }
  410.  
  411. #ifndef C
  412. void View::LoadVertexes(Directory_Entry *Dir, FILE *handle)
  413. #else
  414. void LoadVertexes(Directory_Entry *Dir, FILE *handle)
  415. #endif
  416. {
  417. #ifdef C
  418.    int i;
  419. #endif
  420.  
  421.    NumVertexes = (short)(Dir->size / sizeof(vertex));
  422.    printf("VERTEXES : %d\n", NumVertexes);
  423.  
  424. #ifndef C
  425.    Vertex_Array = new vertex[NumVertexes];
  426. #else
  427.    if ((Vertex_Array = malloc(Dir->size)) == NULL)
  428.       exit(-1);
  429. #endif
  430.  
  431.    fseek(handle, Dir->foffset, SEEK_SET);
  432.    fread(Vertex_Array, (unsigned int)Dir->size, 1L, handle);
  433.  
  434. #ifndef C
  435.    for(int i = 0;i < NumVertexes;i++) {
  436. #else
  437.    for(i = 0;i < NumVertexes;i++) {
  438. #endif
  439.       fix_w(Vertex_Array[i].x);
  440.       fix_w(Vertex_Array[i].y);
  441.    }
  442. }
  443.  
  444. #ifndef C
  445. void View::LoadSSectors(Directory_Entry *Dir, FILE *handle)
  446. #else
  447. void LoadSSectors(Directory_Entry *Dir, FILE *handle)
  448. #endif
  449. {
  450. #ifdef C
  451.    int i;
  452. #endif
  453.  
  454.    NumSSectors = (short)(Dir->size / sizeof(ssector));
  455.    printf("SSECTORS : %d\n", NumSSectors);
  456.  
  457. #ifndef C
  458.    SSector_Array = new ssector[NumSSectors];
  459. #else
  460.    if ((SSector_Array = malloc(Dir->size)) == NULL)
  461.       exit(-1);
  462. #endif
  463.  
  464.    fseek(handle, Dir->foffset, SEEK_SET);
  465.    fread(SSector_Array, (unsigned int)Dir->size, 1L, handle);
  466.  
  467. #ifndef C
  468.    for(int i = 0;i < NumSSectors;i++) {
  469. #else
  470.    for(i = 0;i < NumSSectors;i++) {
  471. #endif
  472.       fix_w(SSector_Array[i].num_segs);
  473.       fix_w(SSector_Array[i].first_seg);
  474.    }
  475. }
  476.  
  477. /* Returns the screen mode to normal text. */
  478. #ifndef C
  479. void View::Close(void)
  480. #else
  481. void Close(void)
  482. #endif
  483. {
  484. }
  485.